﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;

using KaixinRobot;

namespace KaixinHouse
{
            
    public class UserMoveRecord
    {
        private static Collection<UserMoveRecord> UserMoveRecList = null;
        private static string umrfn = "UMR.log";

        protected string userIdx;
        protected DateTime moveTime = System.DateTime.Now.ToLocalTime();


        protected UserMoveRecord(string uid)
        {
            this.userIdx = uid;
        }

        protected void refresh()
        {
            this.moveTime  = System.DateTime.Now.ToLocalTime();
        }

        private static Collection<UserMoveRecord> loadFromFile()
        {
            Collection<UserMoveRecord> umrl = new Collection<UserMoveRecord>();

            try
            {
                string[] lines = File.ReadAllLines(umrfn);
                foreach (string line in lines)
                {
                    List<int> list = TextParser.GetIntegerList(line);
                    if (list.Count != 7) continue;

                    UserMoveRecord umr = new UserMoveRecord("" + list[0]);
                    umr.moveTime = new DateTime(list[1], list[2], list[3], list[4], list[5], list[6]);
                    umrl.Add(umr);
                }
            }
            catch (System.IO.FileNotFoundException){}

            return umrl;
        }

        private static bool saveToFile()
        {
            Collection<UserMoveRecord> umrl = UserMoveRecList;
            if (umrl.Count <= 0) return false;

            StreamWriter sw = new StreamWriter(umrfn, false);

            foreach (UserMoveRecord umr in umrl)
            {
                string str = string.Format("{0},{1},{2},{3},{4},{5},{6}", 
                    umr.userIdx, umr.moveTime.Year, umr.moveTime.Month, umr.moveTime.Day,
                    umr.moveTime.Hour, umr.moveTime.Minute, umr.moveTime.Second );

                sw.WriteLine(str);
                sw.Flush();
            }

            sw.Close();
            return true;
        }

        private static UserMoveRecord getUMR( string uid )
        {
            foreach (UserMoveRecord umr in UserMoveRecList)
            {
                if (umr.userIdx.Equals(uid)) return umr;
            }
            return null;
        }

        public static void touch( string uid )
        {
            if (UserMoveRecList == null) UserMoveRecList = loadFromFile();
            if (string.IsNullOrEmpty(uid)) return;

            UserMoveRecord curUMR = getUMR(uid);
            if (curUMR == null)
            {
                curUMR = new UserMoveRecord(uid);
                UserMoveRecList.Add(curUMR);
            }

            curUMR.refresh();

            saveToFile();
        }

        //根据_RunTime.UserMoveRecList对Slave列表，按照时间先后进行排序
        public static Collection<UserIdentity> SortByUMR(Collection<UserIdentity> uil)
        {
            if (UserMoveRecList == null) UserMoveRecList = loadFromFile();
            UserIdentity tmp = null;

            //对列表进行冒泡排序，时间最久的人排在最前面

            int i = 0, j = 0, MxLen = uil.Count;
            for (i = 0; i < MxLen; i++)
            {
                for (j = i + 1; j < MxLen; j++)
                {
                    if (isEarlierThan(getUMR(uil[j].userIdx), getUMR(uil[i].userIdx)))
                    {
                        tmp = uil[j];
                        uil[j] = uil[i];
                        uil[i] = tmp;
                    }
                }
            }

            //打印调试信息
            foreach (UserIdentity uid in uil)
            {
                UserMoveRecord umri = getUMR(uid.userIdx);
                if (umri != null)
                {
                    TraceLog.PrintLn("  * UMR SortResult: {0} @ {1}", umri.userIdx, umri.moveTime);
                }
                else
                {
                    TraceLog.PrintLn("  * UMR SortResult: {0} @ Not recorded", uid.userIdx);
                }
            }

            return uil;
        }

        private static bool isEarlierThan(UserMoveRecord u1, UserMoveRecord u2)
        {
            //视空值为最悠久的记录

            if ((u1 == null) && (u2 == null)) return false;
            if ((u1 != null) && (u2 == null)) return false;
            if ((u1 == null) && (u2 != null)) return true;

            return (u1.moveTime < u2.moveTime);
        }
    }

}
